﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Pong
{
    class Pallete
    {
        Texture2D texture;
        Vector2 position;
        public Rectangle size;
        Vector2 velocity;

        public int points = 0;

        public Pallete(Texture2D texture, Vector2 position)
        {
            this.texture = texture;
            this.position = position;
            this.velocity = new Vector2(0, 5);
            this.size = texture.Bounds;
        }

        public void Update(
            GameTime gameTime,
            GameWindow janela, 
            List<Keys> teclas
            )
        {
            #region Manter na Janela

            ManterNaTela(janela);
            
            #endregion

            foreach (Keys tecla in teclas)
            {
                if (Keyboard.GetState().IsKeyDown(tecla) 
                    && tecla == Keys.Down)
                {
                    position+=velocity;
                }

                if (Keyboard.GetState().IsKeyDown(tecla)
                    && tecla == Keys.Up)
                {
                    position -= velocity;
                }

                if (Keyboard.GetState().IsKeyDown(tecla)
                    && tecla == Keys.Z)
                {
                    position += velocity;
                }

                if (Keyboard.GetState().IsKeyDown(tecla)
                    && tecla == Keys.A)
                {
                    position -= velocity;
                }
            }

            //atualiza a posicao do rect da bola de acordo com  a sua posicao 
            this.size.X  = (int) position.X ;
            this.size.Y = (int) position.Y;    

        }

        public void ManterNaTela(GameWindow janela)
        {
            if (position.Y <= 0)
            {
                position.Y = 0;
            }

            if (position.X <= 0)
            {
                position.X = 0;
            }

            if (position.Y >= janela.ClientBounds.Height - size.Height)
            {
                position.Y = janela.ClientBounds.Height - size.Height;
            }

            if (position.X >= janela.ClientBounds.Width - size.Width)
            {
                position.X = janela.ClientBounds.Width - size.Width;
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, Color.White);
        }

        public void palleteIA (Ball ball)
        {
            if ((position.Y > ball.Position.Y - size.Height / 2) && (position.X > 400))
            {
                             position-=velocity;
            }
             if ((position.Y < ball.Position.Y - size.Height / 2) && (position.X > 400))
                 position += velocity;
        }


    }
}